                                                                                                                            function statusFormatter(value,row,index) {
        if(value==undefined||value == "") return "";
                        else if(value==1)
            return "未开启";
                else if(value==2)
            return "进行中";
                else if(value==3)
            return "拼团成功";
                else if(value==4)
            return "拼团失败";
                else if(value==5)
            return "活动结束";
                        else return ""
    }
                                                                                                                                                                                                                                                        
function changeState(id, state) {
    $.ajax({
        type: 'POST',
        url: "changeState",
        data: {
            id: id,
            state: state
        },
        success: function () {
            $('#mytable').bootstrapTable('refresh', {url: '/admin/pintuanopen/list'});
        },
        dataType: "json"
    });
}

function deleteOne(id) {
    swal({
        title: '您确定要删除此行数据？',
        text: '你将无法恢复它！',
        type: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#ed5666',
        confirmButtonText: '确定删除！',
        cancelButtonText: '取消删除！',
        confirmButtonClass: 'btn btn-success',
        cancelButtonClass: 'btn btn-danger',
        buttonsStyling: false
    }).then(function(){

        $.ajax({
            type: 'POST',
            url: "deleteOne",
            data: {
                id: id,
            },
            success: function (status) {
                if (status == 1) {
                    swal({title: "删除成功", text: "", type: "success"})
                    $('#mytable').bootstrapTable('refresh', {url: '/admin/pintuanopen/list'});
                }else {
                    swal({title: "删除失败", text: "", type: "error"})
                }
            },
            dataType: "json"
        });

    }, function(dismiss) {
        // dismiss的值可以是'cancel', 'overlay',
        // 'close', 'timer'
        if (dismiss === 'cancel') {
            swal({title: '您已取消操作！',text: '',timer: 2000})
        }
    })
}

var editsw=[];
/**
 * 为模态框填充数据
 */
$('#modal-form').on('show.bs.modal', function (event) {
    var button = $(event.relatedTarget); // 触发事件的按钮
    var index = button.data('index');
    var modal = $(this);
    var data = $('#mytable').bootstrapTable("getData")[index]
    console.log(data)

                        modal.find('#id').val(data.id)
                                modal.find('#ptid').val(data.ptid)
                                modal.find('#gid').val(data.gid)
                                                                        modal.find('#mpid').val(data.mpid)
                                modal.find('#phone').val(data.phone)
                                modal.find('#nickname').val(data.nickname)
                                modal.find('#status').find("option[value='"+data['status']+"']").attr("selected",true);
                                modal.find('#starttime').val(data.starttime)
                                modal.find('#endtime').val(data.endtime)
                                $('#sninfo2').code(data.info);
            $('#info2').val(data.info);
                                modal.find('#ptotal').val(data.ptotal)
                                modal.find('#pnumber').val(data.pnumber)
                                modal.find('#price').val(data.price)
                                modal.find('#pinprice').val(data.pinprice)
                                modal.find('#cheapprice').val(data.cheapprice)
                                editsw["isGroups"]==undefined ? editsw["isGroups"] = new Switchery(document.querySelector('#isGroups'), {color: "#1AB394"}):""
            if(data.isGroups==1) {
                setSwitchery(editsw["isGroups"], true);
            }else{
                setSwitchery(editsw["isGroups"], false);
            }
                                modal.find('#successtime').val(data.successtime)
                                                                        modal.find('#modifier').val(data.modifier)
            
})

//tableHeight函数
function tableHeight(){
    //可以根据自己页面情况进行调整
    return $(window).height() -200;
}

$(function() {

    var sw = [];
    $(".js-switch").each(function (i,e) {
        sw[i] = new Switchery(e, {color: "#1AB394"})
    })
    $(window).resize(function () {
        $('#mytable').bootstrapTable('resetView', {
            height: tableHeight()
        })
    })
    //生成用户数据
    $('#mytable').bootstrapTable({
        method: 'get',
        url:"/admin/pintuanopen/list",
        // height:520,//高度调整
        // toolbar: '#toolbar',
        striped: true, //是否显示行间隔色
        dataField: "records",
        pagination:true,//是否分页
        queryParamsType:'limit',
        queryParams:queryParams,
        sidePagination:'server',
        pageNumber: 1, //初始化加载第一页，默认第一页
        pageSize:50,//单页记录数
        pageList:[50,100,200,500],//分页步进值
        showExport:true,//显示导出
        showRefresh:false,//刷新按钮
        showColumns:false,
        clikToSelect: true,//是否启用点击选中行
        // toolbarAlign:'right',
        // buttonsAlign:'right',//按钮对齐方式
        columns:[
             {
                 title:'全选',
                 field:'select',
                 checkbox:true,
                 width:25,
                 align:'center',
                 valign:'middle'
            },
            {
                title:'ID',
                field:'id',
                visible:false
            },
                                                                                                                                                                                                        {
                            title:'拼团id',
                            field:'ptid'
                        },
                                                                                                                                                                                                                                                        {
                            title:'商品id',
                            field:'gid'
                        },
                                                                                                                                                                                                                                                                                                    {
                            title:'手机号',
                            field:'phone'
                        },
                                                                                                                                                                                                                                                        {
                            title:'昵称',
                            field:'nickname'
                        },
                                                                                                                                                                                                                                                                                                                            {
                            title:'业务状态',
                            field:'status',
                            formatter:"statusFormatter"
                        },
                                                                                                                                                                                                                                                        {
                            title:'开团时间',
                            field:'starttime'
                        },
                                                                                                                                                                                                                                                        {
                            title:'结束时间',
                            field:'endtime'
                        },
                                                                                                                                                                                                                                                                                                                                                                                                                                                {
                            title:'开团设置总人数',
                            field:'ptotal'
                        },
                                                                                                                                                                                                                {
                            title:'已参团人数',
                            field:'pnumber'
                        },
                                                                                                                                                                                                                                                        {
                            title:'原始价格',
                            field:'price'
                        },
                                                                                                                                                                                                                                                        {
                            title:'拼团价格',
                            field:'pinprice'
                        },
                                                                                                                                                                                                                                                        {
                            title:'拼团优惠',
                            field:'cheapprice'
                        },
                                                                                                                                                                                                                                                        {
                            title:'是否组团',
                            field:'isGroups',
                            formatter:"sfFormatter"
                        },
                                                                                                                                                                                                                {
                            title:'拼团成功时间',
                            field:'successtime'
                        },
                                                                                                                                                {
                title:'操作',
                field:'Attribute',
                align:'center',
                width: 170,
                formatter:actionFormatter
            }
        ],
        locale:'zh-CN',//中文支持,
    })

    //请求服务数据时所传参数
    function queryParams(params){
        console.log(params)
        return params;
    }

    /*
     * 验证错误提示信息高亮
     */
    $.validator.setDefaults({
        highlight: function (e) {
            $(e).closest(".form-group").removeClass("has-success").addClass("has-error")
        }, success: function (e) {
            e.closest(".form-group").removeClass("has-error").addClass("has-success")
        }, errorElement: "span", errorPlacement: function (e, r) {
            e.appendTo(r.is(":radio") || r.is(":checkbox") ? r.parent().parent().parent() : r.parent())
        }, errorClass: "help-block m-b-none", validClass: "help-block m-b-none"
    }),$("#aform").validate(),$("#mform").validate();
})

function checkAdd() {
    $('input:checkbox').each(function (index, item) {
        if ($(this).prop("checked")) {
            $(this).val(1);
            console.log($(this).attr("name")+"="+$(this).val())
        } else {
            $(this).val(-1);
            console.log($(this).attr("name")+"="+$(this).val())
        }
    });

                                                                                                                                                                                                                                                                                                                                                                                                $('#info').val($('#sninfo').code())
                                                                                                                                                                                                                                                                                                                                                                                                return true;
}

function checkModify() {
    $('input:checkbox').each(function (index, item) {
        if ($(this).prop("checked")) {
            $(this).val(1);
            console.log($(this).attr("id")+"="+$(this).val())
        } else {
            $(this).val(-1);
            console.log($(this).attr("id")+"="+$(this).val())
        }
    });

                                                                                                                                                                                                                                                                                                                                                                                                $('#info2').val($('#sninfo2').code())
                                                                                                                                                                                                                                                                                                                                                                                                return true;
}